package cn.car91.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class CheckStatusUtil {

    /**
     * 查询网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 查询Wifi是否可用
     *
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (mWiFiNetworkInfo != null) {
                return mWiFiNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 查询手机网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isMobileConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mMobileNetworkInfo != null) {
                return mMobileNetworkInfo.isAvailable();
            }
        }
        return false;
    }
    
    /**
     * 区别是4g、3g、2g
     * 4g返回4、3g返回3、2g返回2
     * 
     * @param context
     * @return
     */
    public static int isFastMobileNetwork(Context context) {
    	TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    	switch (telephonyManager.getNetworkType()) {
    	case TelephonyManager.NETWORK_TYPE_1xRTT:
    		return 2; // ~ 50-100 kbps
    	case TelephonyManager.NETWORK_TYPE_CDMA:
    		return 2; // ~ 14-64 kbps
    	case TelephonyManager.NETWORK_TYPE_EDGE:
    		return 2; // ~ 50-100 kbps
    	case TelephonyManager.NETWORK_TYPE_EVDO_0:
    		return 3; // ~ 400-1000 kbps
    	case TelephonyManager.NETWORK_TYPE_EVDO_A:
    		return 3; // ~ 600-1400 kbps
    	case TelephonyManager.NETWORK_TYPE_GPRS:
    		return 2; // ~ 100 kbps
    	case TelephonyManager.NETWORK_TYPE_HSDPA:
    		return 3; // ~ 2-14 Mbps
    	case TelephonyManager.NETWORK_TYPE_HSPA:
    		return 3; // ~ 700-1700 kbps
    	case TelephonyManager.NETWORK_TYPE_HSUPA:
    		return 3; // ~ 1-23 Mbps
    	case TelephonyManager.NETWORK_TYPE_UMTS:
    		return 3; // ~ 400-7000 kbps
    	case TelephonyManager.NETWORK_TYPE_EHRPD:
    		return 3; // ~ 1-2 Mbps
    	case TelephonyManager.NETWORK_TYPE_EVDO_B:
    		return 3; // ~ 5 Mbps
    	case TelephonyManager.NETWORK_TYPE_HSPAP:
    		return 3; // ~ 10-20 Mbps
    	case TelephonyManager.NETWORK_TYPE_IDEN:
    		return 2; // ~25 kbps
    	case TelephonyManager.NETWORK_TYPE_LTE:
    		return 4; // ~ 10+ Mbps
    	default:
    		return 0;
    	}
	}

    /**
     * 查询当前网络连接信息
     *
     * @param context
     * @return
     */
    public static int getConnectedType(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
                return mNetworkInfo.getType();
            }
        }
        return -1;
    }
}
